package modelimplementation

import (
	"salary/model"
	"time"
)

type UnionAffiliation struct {
	memberId      	int
	dues          	float64
	serviceCharge 	map[time.Time]ServiceCharge
}

func NewUnionAffiliation(memberId int, dues float64) *UnionAffiliation {
	return &UnionAffiliation{memberId: memberId, dues: dues}
}

func (ua *UnionAffiliation) GetServiceCharge(date time.Time) ServiceCharge {
	return ua.serviceCharge[date]
}

func (ua *UnionAffiliation) GetDues() float64 {
	return ua.dues
}

func (ua *UnionAffiliation) GetMemberId() int {
	return ua.memberId
}

func (ua *UnionAffiliation) AddServiceCharge(date time.Time, amount float64) {
	serviceCharge := NewServiceCharge(date, amount)
	ua.serviceCharge[date] = *serviceCharge
}
// 计算星期五的数量
func (ua *UnionAffiliation) numberOfFridayInPayPeriod(payPeriodStart, payPeriodEnd time.Time) int {
	fridays := 0
	for date := payPeriodStart; date.Before(payPeriodEnd); date.AddDate(0, 0, 1) {
		if date.Weekday() == time.Friday {
			fridays++
		}
	}
	return fridays
}
// 计算扣费
func (ua *UnionAffiliation) CalculateDeductions(pc model.PayCheck) float64 {
	totalServiceCharge := 0.0
	totalDues := 0.0
	for date, serviceCharge := range ua.serviceCharge {
		date1 := pc.GetPayPeriodStartDate()
		date2 := pc.GetPayPeriodEndDate()
		if date.After(date1) && date.Before(date2) {
			totalServiceCharge += serviceCharge.GetAmount()
		}
	}
	fridays := ua.numberOfFridayInPayPeriod(pc.GetPayPeriodStartDate(), pc.GetPayPeriodEndDate())
	totalDues = ua.dues * float64(fridays)
	return totalDues + totalServiceCharge
}